/**
 * Copyright (c) 2018, 西安星沙网络科技-版权所有
 *
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.sstech.member.config;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import cn.sstech.member.core.catche.RedisSessionDao;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class ShiroConfiguration {

	private static final Logger mLog = LoggerFactory.getLogger(ShiroConfiguration.class);

	@Value("${spring.redis.host}")  
    private String host;  
    @Value("${spring.redis.port}")  
    private int port;  
    @Value("${spring.redis.timeout}")  
    private int timeout;  
    @Value("${spring.redis.password}")  
    private String password;
    @Value("${spring.redis.expire}")
    private int expire;
    
    @Bean
    public StringRedisSerializer stringRedisSerializer(){
    	StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    	return stringRedisSerializer;
    }
    
    @Bean
    public JdkSerializationRedisSerializer jdkSerializationRedisSerializer(){
    	JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
    	return jdkSerializationRedisSerializer;
    }
    
    @Bean  
    public DefaultWebSessionManager sessionManager() {  
    	
    	DefaultWebSessionManager defaultWebSessionManager = new DefaultWebSessionManager();
    	defaultWebSessionManager.setSessionValidationInterval(1800000);
    	defaultWebSessionManager.setGlobalSessionTimeout(1800000);
    	defaultWebSessionManager.setDeleteInvalidSessions(true);
    	defaultWebSessionManager.setSessionValidationSchedulerEnabled(true);
    	defaultWebSessionManager.setSessionDAO(sessionDAO());
    	
    	
        return defaultWebSessionManager;  
    }  
    
    @SuppressWarnings("rawtypes")
	@Bean  
    public RedisSessionDao sessionDAO() {  
    	RedisSessionDao redisSessionDao = new RedisSessionDao();
    	redisSessionDao.setRedisTemplate(redisObjectTemplate());
    	return redisSessionDao;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean  
    public RedisTemplate redisObjectTemplate() {  
    	RedisTemplate rRedisTemplate = new RedisTemplate();
    	rRedisTemplate.setConnectionFactory(jedisConnectionFactory());
    	rRedisTemplate.setKeySerializer(stringRedisSerializer());
    	rRedisTemplate.setValueSerializer(jdkSerializationRedisSerializer());
    	rRedisTemplate.setHashKeySerializer(stringRedisSerializer());
    	rRedisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer());
    	return rRedisTemplate;
    }
    
	@Bean  
    public JedisPoolConfig jedisPoolConfig() {  
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(600);
		jedisPoolConfig.setMaxIdle(300);
		jedisPoolConfig.setMinIdle(10);
		jedisPoolConfig.setMaxWaitMillis(2000);
		jedisPoolConfig.setTestOnBorrow(true);
		jedisPoolConfig.setTestOnReturn(true);
    	return jedisPoolConfig;
    }
	
	@Bean
    public JedisConnectionFactory jedisConnectionFactory(){
    	JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    	jedisConnectionFactory.setHostName(host);
    	jedisConnectionFactory.setPort(port);
    	jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
    	jedisConnectionFactory.setTimeout(timeout);
    	jedisConnectionFactory.setUsePool(true);
    	jedisConnectionFactory.setPassword(password);
    	jedisConnectionFactory.setDatabase(0);
    	return jedisConnectionFactory;
    }
}
